package hashset;

import hashtable.MinhaHashTable;
import iterator.MeuListIterator;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import list.MeuArrayList;


public class MeuHashSet<T> implements Set<T>{
	private MinhaHashTable<T, Object> mapa;
	
	private static int TAMANHO_PADRAO = 10;
	
	private static Object ELEMENTO_PRESENTE = new Object();
	
	public MeuHashSet(Integer tamanhoInicial) {
		mapa = new MinhaHashTable<T, Object>(tamanhoInicial);
	}
	
	public MeuHashSet() {
		this(TAMANHO_PADRAO);
	}
	
	@Override
	public int size() {
		return mapa.size();
	}
	
	@Override
	public boolean isEmpty() {
		return size() == 0;
	}

	@Override
	public boolean contains(Object o) {
		return mapa.containsKey(o);
	}

	@Override
	public Object[] toArray() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T[] toArray(T[] a) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean add(T e) {
		return mapa.put(e, ELEMENTO_PRESENTE) == null;
	}

	@Override
	public boolean remove(Object o) {
		return mapa.remove(o, ELEMENTO_PRESENTE) == true;
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		for (Object object : c) {
			if (!contains(object)) {
				return false;
			}
		}
		
		return true;
	}

	@Override
	public boolean addAll(Collection<? extends T> c) {
		boolean alterou = false;
		for (T t : c) {
			if(add(t)) {
				alterou  = true;
			}
		}
		
		return alterou;
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void clear() {
		this.mapa.clear();
	}
	
	@Override
	public String toString() {
		
		StringBuilder builder = new StringBuilder("[");
		
		for (T elemento : this.mapa.keySet()) {
			builder.append(elemento + ", ");
		}
		return builder.append("]").toString();
	}
	
	@Override
	public Iterator<T> iterator() {
		MeuArrayList<Object> lista = new MeuArrayList<Object>();
		
		for (T elemento : this.mapa.keySet()) {
			lista.add(elemento);
		}
		
		return new MeuListIterator<T>(lista, lista.size());
	}
}
